import { getAllRoleList, setAdminStatus } from '/@/api/demo/system'
import { BasicColumn } from '/@/components/Table'
import { FormSchema } from '/@/components/Table'
import { h } from 'vue'
import { Switch } from 'ant-design-vue'
import { useMessage } from '/@/hooks/web/useMessage'

export const columns: BasicColumn[] = [
  {
    title: '用户名',
    dataIndex: 'username',
    width: 120,
  },
  {
    title: '角色',
    dataIndex: 'role_id',
    width: 200,
    customRender: ({ record }) => {
      return record.role.role_name
    },
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 120,
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'pendingStatus')) {
        record.pendingStatus = false
      }
      return h(Switch, {
        checked: record.status === 1,
        checkedChildren: '已启用',
        unCheckedChildren: '已禁用',
        loading: record.pendingStatus,
        onChange(checked: boolean) {
          record.pendingStatus = true
          const newStatus = checked ? 1 : 0
          const { createMessage } = useMessage()
          setAdminStatus(record.id, newStatus)
            .then(() => {
              record.status = newStatus
              createMessage.success(`已成功修改管理员状态`)
            })
            .catch(() => {
              createMessage.error('修改管理员状态失败')
            })
            .finally(() => {
              record.pendingStatus = false
            })
        },
      })
    },
  },
  {
    title: '昵称',
    dataIndex: 'nickname',
    width: 120,
  },
  {
    title: '邮箱',
    dataIndex: 'email',
    width: 120,
  },
  {
    title: '创建时间',
    dataIndex: 'created_at',
    width: 180,
  },
  {
    title: '备注',
    dataIndex: 'description',
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    field: 'username',
    label: '用户名',
    component: 'Input',
    colProps: { span: 5 },
  },
  {
    field: 'nickname',
    label: '昵称',
    component: 'Input',
    colProps: { span: 5 },
  },
  {
    label: '角色',
    field: 'role_id',
    component: 'ApiSelect',
    colProps: { span: 6 },
    componentProps: {
      api: getAllRoleList,
      labelField: 'role_name',
      valueField: 'id',
    },
  },
  {
    field: 'created_at',
    label: '创建日期',
    component: 'RangePicker',
    componentProps: {
      format: 'YYYY-MM-DD HH:mm:ss',
      placeholder: ['开始时间', '结束时间'],
      showTime: { format: 'HH:mm:ss' },
    },
    colProps: { span: 8 },
  },
]

export const accountFormSchema: FormSchema[] = [
  {
    field: 'username',
    label: '登录名',
    component: 'Input',
    helpMessage: ['检测登录名是否存在'],
    rules: [
      {
        required: true,
        message: '请输入用户名',
      },
    ],
  },
  {
    field: 'nickname',
    label: '昵称',
    component: 'Input',
    required: true,
  },

  {
    field: 'password',
    label: '密码',
    component: 'InputPassword',
    helpMessage: ['留空则不修改密码'],
    required: false,
    ifShow: true,
  },

  {
    label: '角色',
    field: 'role_id',
    component: 'ApiSelect',
    componentProps: {
      api: getAllRoleList,
      labelField: 'role_name',
      valueField: 'id',
    },
    required: true,
  },

  {
    label: '邮箱',
    field: 'email',
    component: 'Input',
    required: false,
    defaultValue: '',
  },

  {
    label: '描述',
    field: 'description',
    component: 'InputTextArea',
    defaultValue: '',
  },
]
